package com.publishproject.test;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.example.myutils.imagescan.GalleryImagesActivity;
import com.example.myutils.imagescan.utils.ImageLoaderUtil;
import com.publishproject.R;

import java.util.ArrayList;
import java.util.List;

public class GalleryActivity extends AppCompatActivity {

    private ArrayList<String> mList = new ArrayList<>();
    private GridView gridView;
    private Adapter adapter;
    private ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery_images);
        gridView = (GridView)findViewById(R.id.gridView);
        adapter = new Adapter();
        gridView.setAdapter(adapter);
        image = (ImageView)findViewById(R.id.imageView);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode != RESULT_OK)return;
        switch (requestCode){
            case 1000:
                List<String> list = (ArrayList<String>) data.getSerializableExtra(GalleryImagesActivity.RESULT_LIST);
                mList.clear();
                if(list == null){
                    list = new ArrayList<>();
                }
                mList.addAll(list);
                adapter.notifyDataSetChanged();
                break;
        }
    }

    class Adapter extends BaseAdapter{

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(GalleryActivity.this);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,100));
            ImageLoaderUtil.displayImage("file://"+mList.get(position),imageView);
            return imageView;
        }
    }
}
